//
//  WFCaseCell.m
//  谷居
//
//  Created by dllo on 16/6/17.
//  Copyright © 2016年 刘万方. All rights reserved.
//

#import "WFCaseCell.h"
#import "WFHandleURL.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "UIImage+AFNetworking.h"
#import "WFProjectModel.h"
#import "WFSaveTool.h"
#import "Masonry.h"

@interface WFCaseCell ()
@property (nonatomic, assign) BOOL isSave;
@end

@implementation WFCaseCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCell];
        
    }
    return self;
}
- (void)createCell {
    _backgoundView = [[WFCaseBacgroundView alloc] init];
    [self.contentView addSubview:_backgoundView];
    [_backgoundView release];
    
    _mainImageView = [[WFMainImageViewOfCell alloc] init];
    [_backgoundView addSubview:_mainImageView];
    [_mainImageView release];
    
    _buildNameLabel = [[WFBuildNameLabel alloc] init];
    [_backgoundView addSubview:_buildNameLabel];
    [_buildNameLabel release];
    
    _likeButton = [[WFLikeButton alloc] init];
    [_backgoundView addSubview:_likeButton];
    [_likeButton release];
    
    _produceLabel = [[WFProduceLabel alloc] init];
    [_backgoundView addSubview:_produceLabel];
    [_produceLabel release];
    
    _PhototView = [[WFPhotoView alloc] init];
    [_backgoundView  addSubview:_PhototView];
    [_PhototView release];
    
    _titleLabel = [[WFTitleLabel alloc] init];
    [_backgoundView addSubview:_titleLabel];
    [_titleLabel release];
    
    
    
    
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _backgoundView.frame = CGRectMake(0, 0, SELF_WIDTH, SELF_HEIGHT);
//    _backgoundView.backgroundColor = NIGHT_COLOR;
    
    _mainImageView.frame = CGRectMake(0, 0, SELF_WIDTH, 180);
    NSString *picUrl = [WFHandleURL getPicURL:_projectModel.coverPhoto];
    NSLog(@"styleCell: %d", self.styleCell);
    if (!self.styleCell) {
        _mainImageView.image = self.mainImage;
        _PhototView.image = self.photoImage;
        NSLog(@"使用的是本地图篇. ");
    } else {
        NSLog(@"使用网络图片");
    [_mainImageView setImageWithURL:[NSURL URLWithString:picUrl] placeholderImage:[UIImage imageNamed:@"图片加载.png"]];
        [_PhototView setImageWithURL:[NSURL URLWithString:_userImageModel.small] placeholderImage:[UIImage imageNamed:@"图片加载.png"]];

    }
    
    [_likeButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_backgoundView).offset(20);
        make.right.equalTo(_backgoundView).offset(-20);
        make.size.mas_equalTo(CGSizeMake(30, 30));
        
    }];
    [_likeButton addTarget:self action:@selector(clickLikeButton:) forControlEvents:UIControlEventTouchUpInside];
//    [_likeButton setBackgroundImage:[UIImage imageNamed:@"喜欢1.png"] forState:UIControlStateNormal];
    
#pragma mark - 判断是否被收藏过
//    self.isSave = 
    if ([WFSaveTool  isModekSaved:_projectModel]) {
        [_likeButton setBackgroundImage:[UIImage imageNamed:@"喜欢2.png"] forState:UIControlStateNormal];
    } else {
        
        [_likeButton setBackgroundImage:[UIImage imageNamed:@"喜欢1.png"] forState:UIControlStateNormal];
    }
    
    
    
    
    _buildNameLabel.frame = CGRectMake(15, 15, 80, 20);
    _buildNameLabel.backgroundColor = [UIColor blackColor];
    _buildNameLabel.alpha = 0.6;
    _buildNameLabel.text = _projectModel.buildingName;
    _buildNameLabel.font = [UIFont systemFontOfSize:15];
    _buildNameLabel.textColor = [UIColor whiteColor];
    [_buildNameLabel sizeToFit];
    
    
    
    
    _PhototView.frame = CGRectMake(320, 160, 40, 40);
    _PhototView.backgroundColor = [UIColor greenColor];
    
    _PhototView.layer.masksToBounds = YES;  /** 告诉layer在他之下的layer都遮住. */
    _PhototView.layer.cornerRadius = 20;

    
    _titleLabel.frame = CGRectMake(10, 185, 300, 20);
    _titleLabel.text = _projectModel.title;
    
    _produceLabel.frame = CGRectMake(10, 210, 300, 20);
    _produceLabel.text = [NSString stringWithFormat:@"%@·%@·%@平米·%@万元", _projectModel.styleShow, _projectModel.typeShow,_projectModel.areaShow, _projectModel.costShow];
    
  
    
}


- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    
}
- (void)clickLikeButton:(UIButton *)likeButton {
    if ([WFSaveTool isModekSaved:_projectModel]) {
        [WFSaveTool removeModelFromDocument:_projectModel];
    } else {
        [WFSaveTool addModelToDocument:_projectModel];
    }
    [self.delegate tableViewReloadData];
    
}

//- (void)changeLikeButtonColorWtihImageView:(UIImage *)image {
//    [_likeButton setBackgroundImage:image forState:UIControlStateNormal];
//}

- (void)dealloc {
    [super dealloc];
}
@end
